<?php
$this->breadcrumbs=array(
		'Quản trị'=>array('/admin'),
		'Sản phẩm'=>array('index'),
		'Danh sách',
);

$this->menu=array(
		array('label'=>'Đăng sản phẩm', 'url'=>array('create')),
);

Yii::app()->clientScript->registerScript('search', "
		$('.search-button').click(function(){
		$('.search-form').toggle();
		return false;
});
		$('.search-form form').submit(function(){
		$.fn.yiiGridView.update('product-grid', {
		data: $(this).serialize()
});
		return false;
});
		");
?>

<h1>Danh sách sản phẩm</h1>


<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display: none">
	<?php $this->renderPartial('_search',array(
			'model'=>$model,
)); ?>
</div>
<!-- search-form -->

<?php 
function displayImg($model){
	if($model->image_id){

		$thumb = '/upload/images/thumb/product50/'.substr_replace($model->image->path,'',0,14);
		if(!is_file(Yii::getPathOfAlias('webroot').$thumb)){
			Yii::import('application.extensions.image.Image');
			$image = new Image(Yii::getPathOfAlias('webroot').'/'.$model->image->path);
			$image->resize(50, 50);
			if(!is_dir(Yii::getPathOfAlias('webroot').'/upload/images/thumb/product50/')){
				mkdir(Yii::getPathOfAlias('webroot').'/upload/images/thumb/product50/');
				chmod(Yii::getPathOfAlias('webroot').'/upload/images/thumb/product50/',0777);
			}
			$image->save(Yii::getPathOfAlias('webroot').$thumb);
		}

		return '<div style="text-align:center;"><img src="'.Yii::app()->baseUrl.$thumb.'"/></div>';
	}
	return 'No image';
}


function getCatName($model){
	//global $arrCat;
	return $model->cat->name;
}

$this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'product-grid',
		'dataProvider'=>$model->search(),
		//'filter'=>$model,
		'columns'=>array(
				'id',
				'name',
				//'description',
				'model',
				array('name'=>'price','header'=>'Giá (VNĐ)','value'=>'number_format($data->price,0,",",".")'),
				array('name'=>'cat_id','value'=>'getCatName($data)'),

				array('name'=>'status','value'=>'$data->status == 1 ? "Hoạt động":"Không hoạt động"'),
				array('name'=>'image_id','value'=>'displayImg($data)','type'=>'html'),

				array(
						'class'=>'CButtonColumn',
				),
		),
)); ?>
